package cn.liyao.im.config;


import cn.liyao.im.event.MessageQoSEventS2CListnerImpl;
import cn.liyao.im.event.ServerEventListenerImpl;
import cn.liyao.im.util.ConversationUtil;
import cn.liyao.web.model.GroupMember;
import cn.liyao.web.repository.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import server.*;
import server.network.*;
import server.qos.QoS4ReciveDaemonC2S;
import server.qos.QoS4SendDaemonS2C;
import server.utils.ServerToolKits;
import sun.plugin2.message.Conversation;

import javax.annotation.PreDestroy;

/**
 * 具体属性含义请参考 application.yml
 */
@Data
@Component
@ConfigurationProperties(prefix = "im")
public class ImConfig implements ApplicationRunner {

    private int udpPort;
    private int tcpPort;
    private int websocketPort;
    private boolean bridgeEnabled;
    private boolean qosSendDaemonDebug;
    private boolean qosReceiveDaemonDebug;
    private String senseModeTcp;
    private String senseModeWebsocket;

    private ServerLauncher serverLauncher;



    @Autowired
    private final MessageRepository messageRepository;


    @Autowired
    private final OfflineMessageRepository offlineMessageRepository;


    @Autowired
    private final GroupMemberRepository groupMemberRepository;


    @Autowired
    private final ConversationRepository conversationRepository;

    @Autowired
    private final UserRepository userRepository;


    @Autowired
    private final GroupRepository groupRepository;

    @Autowired
    private final ConversationUtil conversationUtil;


    @Autowired
    private final FriendRepository friendRepository;

    @Autowired
    private final UserMessageRepository userMessageRepository;

    /**
     * 初始化IM 配置信息
     */
    private void initConfig() {
        // 设置MobileIMSDK服务端的TCP网络监听端口
        GatewayUDP.PORT = udpPort;
        // 设置MobileIMSDK服务端的TCP网络监听端口
        GatewayTCP.PORT = tcpPort;
        // 设置MobileIMSDK服务端的WebSocket网络监听端口
        GatewayWebsocket.PORT = websocketPort;

        // 同时支持 udp, tcp , websocket
        ServerLauncher.supportedGateways = Gateway.SOCKET_TYPE_UDP | Gateway.SOCKET_TYPE_TCP | Gateway.SOCKET_TYPE_WEBSOCKET;

        // 开启日志
        QoS4SendDaemonS2C.getInstance().setDebugable(qosSendDaemonDebug);
        QoS4ReciveDaemonC2S.getInstance().setDebugable(qosReceiveDaemonDebug);
        // 心跳时长
        ServerToolKits.setSenseModeTCP(ServerToolKits.SenseModeTCP.MODE_5S);
        ServerToolKits.setSenseModeWebsocket(ServerToolKits.SenseModeWebsocket.MODE_5S);


    }

    @Override
    public void run(ApplicationArguments args) throws Exception {

        initConfig();
        serverLauncher = new ServerLauncher() {
            @Override
            protected void initListeners() {
                this.setServerEventListener(new ServerEventListenerImpl(messageRepository,offlineMessageRepository,groupMemberRepository,conversationRepository,userRepository,groupRepository,conversationUtil,friendRepository,userMessageRepository));
                this.setServerMessageQoSEventListener(new MessageQoSEventS2CListnerImpl(messageRepository));

            }
        };
        serverLauncher.startup();
    }

    @PreDestroy
    public void shutdown() {
        serverLauncher.shutdown();
    }
}
